<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/24
 * Time: 上午8:59
 */
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class RuleController extends AdminBaseController{
    public function index(){
        $rule = M('rule');
        $list = $rule->field('id,pid,name,title,status')->order('id')->select();
        $list = getTree($list,0,0,'|----');
        $this->assign('list',$list);
        $this->display();
    }



    public function add(){
        if(IS_GET){
            $id = I('get.id/d',0);
            $pid = I('get.pid/d',0);
            $edit = M('rule')->field('id,name,pid,title,status')->where(['id' => $id])->find();
            $this->assign('edit',$edit);
            $this->assign('pid',$pid);
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
        $this->display();
    }

    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            $edit = M('rule')->field('id,name,pid,title,status')->where(['id' => $id])->find();
            $this->assign('edit',$edit);
            $this->display();
        }
    }

    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $title = I("post.title/s",'');
            if(!$title){
                $this->ajaxReturn(['status' => 1, 'msg' => '权限名称不能为空！']);
            }
            $name = I("post.name/s",'');
            if(!$name){
                $this->ajaxReturn(['status' => 1, 'msg' => '权限不能为空！']);
            }
            $status = I('post.status/d',0);
            $pid = I('post.pid/d',0);
            $rule = M('rule');
            $ruleinfo = $rule->where(['title' => $title,'_logic' => 'OR','name' => $name])->count();
            if($ruleinfo > 0 && empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '权限已存在']);
            }
            $data = [
                'name' => $name,
                'title' => $title,
                'id' => $id,
                'pid' => $pid,
                'status' => $status
            ];
            $issql = $rule->add($data,[],true);
            if(!$issql){
                $this->ajaxReturn(['status' => 1,'msg' => '失败']);
            }
            $this->ajaxReturn(['status' => 0,'msg' => '成功']);
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
    public function Del(){
        if(IS_POST){
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' =>'非法操作']);
            }
            $counts = M('rule')->where(['pid' => $id])->count();
            if($counts > 0){
                $this->ajaxReturn(['status' => 1,'msg' => '先删除子权限']);
            }
            $isdel = M('rule')->where(['id' => $id])->delete();
            if(!$isdel){
                $this->ajaxReturn(['status' => 1,'msg' => '删除失败']);
            }
            $this->ajaxReturn(['status' => 0,'msg' => '删除成功']);
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
    public function changeMode(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            if(!$id){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $rule = M('rule')->field('status')->where('id='.$id)->find();
            if(empty($rule['status'])){
                M('rule')->where(['id' => $id])->setField(['status' => 1]);
                $this->ajaxReturn(['status' => 0,'msg' => '已开启','data' => 1]);
            }else{
                M('rule')->where(['id' => $id])->setField(['status' => 0]);
                $this->ajaxReturn(['status' => 0,'msg' => '已关闭','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
}